home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / hydracom.lha / source / dos_file.c < prev    next >
C/C++ Source or Header  |  1994-01-19  |  9KB  |  349 lines

  1. /*=============================================================================
  2.  
  3.                               HydraCom Version 1.00
  4.  
  5.                          A sample implementation of the
  6.                    HYDRA Bi-Directional File Transfer Protocol
  7.  
  8.                              HydraCom was written by
  9.                    Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT
  10.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  11.  
  12.                        The HYDRA protocol was designed by
  13.                  Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT and
  14.                              Joaquim H. Homrighausen
  15.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  16.  
  17.  
  18.   Revision history:
  19.   06 Sep 1991 - (AGL) First tryout
  20.   .. ... .... - Internal development
  21.   11 Jan 1993 - HydraCom version 1.00, Hydra revision 001 (01 Dec 1992)
  22.  
  23.  
  24.   For complete details of the Hydra and HydraCom licensing restrictions,
  25.   please refer to the license agreements which are published in their entirety
  26.   in HYDRACOM.C and LICENSE.DOC, and also contained in the documentation file
  27.   HYDRACOM.DOC
  28.  
  29.   Use of this file is subject to the restrictions contained in the Hydra and
  30.   HydraCom licensing agreements. If you do not find the text of this agreement
  31.   in any of the aforementioned files, or if you do not have these files, you
  32.   should immediately contact LENTZ SOFTWARE-DEVELOPMENT and/or Joaquim
  33.   Homrighausen at one of the addresses listed below. In no event should you
  34.   proceed to use this file without having accepted the terms of the Hydra and
  35.   HydraCom licensing agreements, or such other agreement as you are able to
  36.   reach with LENTZ SOFTWARE-DEVELOMENT and Joaquim Homrighausen.
  37.  
  38.  
  39.   Hydra protocol design and HydraCom driver:         Hydra protocol design:
  40.   Arjen G. Lentz                                     Joaquim H. Homrighausen
  41.   LENTZ SOFTWARE-DEVELOPMENT                         389, route d'Arlon
  42.   Langegracht 7B                                     L-8011 Strassen
  43.   3811 BT  Amersfoort                                Luxembourg
  44.   The Netherlands
  45.   FidoNet 2:283/512, AINEX-BBS +31-33-633916         FidoNet 2:270/17
  46.   arjen_lentz@f512.n283.z2.fidonet.org               joho@ae.lu
  47.  
  48.   Please feel free to contact us at any time to share your comments about our
  49.   software and/or licensing policies.
  50.  
  51. =============================================================================*/
  52.  
  53. #include "hydracom.h"
  54. #ifdef __MSDOS__
  55. #include <fcntl.h>
  56. #include <share.h>
  57. #include <io.h>
  58. #endif
  59.  
  60.  
  61. static boolean dos_sharing = false;
  62.  
  63.  
  64. void dos_sharecheck (void)    /* SHARE installed? set dos_sharing true/false */
  65. {
  66. #if __MSDOS__
  67.         union REGS regs;
  68.  
  69.         regs.x.ax = 0x1000;                             /* DOS Multiplexer   */
  70.         int86(0x2f,®s,®s);                        /* INT 2Fh sub 1000h */
  71.         dos_sharing = (regs.h.al == 0xff) ? true : false;
  72. #endif
  73. }/*dos_sharecheck()*/
  74.  
  75.  
  76. int dos_open (char *pathname, byte create)
  77. {
  78.         register int access;
  79.  
  80. #ifdef __MSDOS__
  81.         access = O_RDWR | O_BINARY;
  82.         if (create) {
  83.            access |= O_CREAT;
  84.            if (create == 2)
  85.               access |= O_TRUNC;
  86.         }
  87.  
  88.         return (open(pathname, access,
  89.                      create ? S_IREAD | S_IWRITE : 0));
  90. #endif
  91. #ifdef __TOS__
  92.         access = O_RDWR;
  93.         if (create) {
  94.            access |= O_CREAT;
  95.            if (create == 2)
  96.               access |= O_TRUNC;
  97.         }
  98.  
  99.         return (open(pathname, access, 0));
  100. #endif
  101.  
  102. #ifdef AMIGA
  103.         access = O_RDWR;
  104.         if (create) {
  105.            access |= O_CREAT;
  106.            if (create == 2)
  107.               access |= O_TRUNC;
  108.         }
  109.  
  110.         return (open(pathname, access, 0));
  111. #endif    /* AMIGA */
  112. }/*dos_open()*/
  113.  
  114.  
  115. int dos_sopen (char *pathname, byte create)
  116. {
  117.         register int access;
  118.  
  119. #ifdef __MSDOS__
  120.         access = O_RDWR | O_BINARY;
  121.         if (create) {
  122.            access |= O_CREAT;
  123.            if (create == 2)
  124.               access |= O_TRUNC;
  125.         }
  126.  
  127.         return (sopen(pathname, access,
  128.                    dos_sharing ? SH_DENYWR : 0,
  129.                    create ? S_IREAD | S_IWRITE : 0));
  130. #endif
  131. #ifdef __TOS__
  132.         access = O_RDWR;
  133.         if (create) {
  134.            access |= O_CREAT;
  135.            if (create == 2)
  136.               access |= O_TRUNC;
  137.         }
  138.  
  139.         return (open(pathname, access, 0));
  140. #endif
  141. #ifdef AMIGA
  142.         access = O_RDWR;
  143.         if (create) {
  144.            access |= O_CREAT;
  145.            if (create == 2)
  146.               access |= O_TRUNC;
  147.         }
  148.  
  149.         return (open(pathname, access, 0));
  150. #endif
  151. }/*dos_sopen()*/
  152.  
  153.  
  154. int dos_sappend (char *pathname, byte create)
  155. {
  156.         register int access;
  157.  
  158. #ifdef __MSDOS__
  159.         access = O_WRONLY | O_APPEND | O_TEXT;
  160.         if (create) {
  161.            access |= O_CREAT;
  162.            if (create == 2)
  163.               access |= O_TRUNC;
  164.         }
  165.  
  166.         return (sopen(pathname, access,
  167.                       dos_sharing ? SH_DENYWR : 0,
  168.                       create ? S_IREAD | S_IWRITE : 0));
  169. #endif
  170. #ifdef __TOS__
  171.         access = O_WRONLY | O_APPEND;
  172.         if (create) {
  173.            access |= O_CREAT;
  174.            if (create == 2)
  175.               access |= O_TRUNC;
  176.         }
  177.  
  178.         return (open(pathname, access, 0));
  179. #endif
  180. #ifdef AMIGA
  181.         access = O_WRONLY | O_APPEND;
  182.         if (create) {
  183.            access |= O_CREAT;
  184.            if (create == 2)
  185.               access |= O_TRUNC;
  186.         }
  187.  
  188.         return (open(pathname, access, 0));
  189. #endif
  190. }/*dos_sappend()*/
  191.  
  192.  
  193. int dos_close (int handle)
  194. {
  195.         return (close(handle));
  196. }/*dos_close()*/
  197.  
  198.  
  199. int dos_lock (int handle, long offset, long len)
  200. {
  201. #ifdef __MSDOS__
  202.         if (dos_sharing)
  203.            while (!lock(handle,offset,len));
  204. #endif
  205.  
  206.         return (0);
  207. }/*dos_lock()*/
  208.  
  209.  
  210. int dos_unlock (int handle, long offset, long len)
  211. {
  212. #ifdef __MSDOS__
  213.         return (dos_sharing ? unlock(handle,offset,len) : 0);
  214. #endif
  215. #ifdef __TOS__
  216.         return 0;
  217. #endif
  218. #ifdef AMIGA
  219.         return 0;
  220. #endif
  221. }/*dos_unlock()*/
  222.  
  223.  
  224. long dos_seek (int handle, long offset, int fromwhere)
  225. {
  226.         return (lseek(handle,offset,fromwhere));
  227. }/*dos_seek()*/
  228.  
  229.  
  230. long dos_tell (int handle)
  231. {
  232. #ifdef __MSDOS__
  233.         return (tell(handle));
  234. #endif
  235. #ifdef __TOS__
  236.         return (lseek(handle,0L,SEEK_CUR));
  237. #endif
  238. #ifdef AMIGA
  239.         return (lseek(handle,0L,SEEK_CUR));
  240. #endif
  241. }/*dos_tell()*/
  242.  
  243.  
  244. int dos_read (int handle, void *buf, word len)
  245. {
  246. #ifdef __MSDOS__
  247.         return (_read(handle,buf,len));
  248. #endif
  249. #ifdef __TOS__
  250.         return ((int) read(handle,buf,len));
  251. #endif
  252. #ifdef AMIGA
  253.         return ((int) read(handle,buf,len));
  254. #endif
  255. }/*dos_read()*/
  256.  
  257.  
  258. int dos_write (int handle, void *buf, word len)
  259. {
  260. #ifdef __MSDOS__
  261.         return (_write(handle,buf,len));
  262. #endif
  263. #ifdef __TOS__
  264.         return ((int) write(handle,buf,len));
  265. #endif
  266. #ifdef AMIGA
  267.         return ((int) write(handle,buf,len));
  268. #endif
  269. }/*dos_write()*/
  270.  
  271.  
  272. /* ----------------------------------------------------------------------------
  273.                         oflags                  mode
  274.         r       O_RDONLY                        don't care
  275.         w       O_CREAT | O_WRONLY | O_TRUNC    S_IWRITE
  276.         a       O_CREAT | O_WRONLY | O_APPEND   S_IWRITE
  277.         r+      O_RDWR                          don't care
  278.         w+      O_RDWR | O_CREAT | O_TRUNC      S_IWRITE | S_IREAD
  279.         a+      O_RDWR | O_CREAT | O_APPEND     S_IWRITE | S_IREAD
  280. ---------------------------------------------------------------------------- */
  281. FILE *sfopen (char *name, char *mode, int shareflag)
  282. {
  283. #ifdef __MSDOS__
  284.         int   fd, access, flags;
  285.         char  *type = mode, c;
  286.         FILE *fp;
  287.  
  288.         if ((c = *type++) == 'r') {
  289.            access = O_RDONLY;
  290.            flags = 0;
  291.         }
  292.         else if (c == 'w') {
  293.            access = O_WRONLY | O_CREAT | O_TRUNC;
  294.            flags = S_IWRITE;
  295.         }
  296.         else if (c == 'a') {
  297.            access = O_RDWR   | O_CREAT | O_APPEND;
  298.            flags = S_IWRITE;
  299.         }
  300.         else
  301.            return (NULL);
  302.  
  303.         c = *type++;
  304.  
  305.         if (c == '+' || (*type == '+' && (c == 't' || c == 'b'))) {
  306.            if (c == '+') c = *type;
  307.            access = (access & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
  308.            flags = S_IREAD | S_IWRITE;
  309.         }
  310.  
  311.         if      (c == 't') access |= O_TEXT;
  312.         else if (c == 'b') access |= O_BINARY;
  313.         else               access |= (_fmode & (O_TEXT | O_BINARY));
  314.  
  315.         if (dos_sharing)
  316.            access |= shareflag;
  317.  
  318.         fd = open(name, access, flags);
  319.  
  320.         if (fd < 0) return (NULL);
  321.  
  322.         if ((fp = fdopen(fd,mode)) == NULL)
  323.            close(fd);
  324.         else if (setvbuf(fp,NULL,_IOFBF,BUFSIZ)) {
  325.            fclose(fp);
  326.            return (NULL);
  327.         }
  328.  
  329.         return (fp);
  330. #endif
  331.  
  332. #ifdef __TOS__
  333.         char *p, *q, nm[10];
  334.  
  335.         strcpy(nm,mode);
  336.         for(p=q=nm; *p; p++) if (*p!='t') *q++=*p;
  337.         *q='\0';
  338.  
  339.         return (fopen(name,nm));
  340. #endif
  341.  
  342. #ifdef AMIGA
  343.     return(fopen(name,mode));
  344. #endif    /* AMIGA */
  345. }/*sfopen()*/
  346.  
  347.  
  348. /* end of dos_file.c */
  349.